<?php

class DisplayHandler {
	private $Request;
	
	function __construct(Request $Request){
		$this->Request = $Request;
	}
	
	function handle(){
		$Template = SystemRegistry::getInstance()->get("template");
		
		$Result = $this->Request->getResult();
		
		$AppController = $this->Request->getApplicationController();
		$AppName = $this->Request->getApplicationName();
		$ViewFileName = $AppController->getView();
		
		if(empty($ViewFileName)){
			return false;
		}
		
		$TemplateDir = dirname(__FILE__)."/../theme/".$Template."/";
		
		$TemplateAppViewFile = $TemplateDir."apps/".$AppName."/".$ViewFileName;
		$AppViewFile = $this->Request->getApplicationDir()."/views/".$ViewFileName;
		
		if(is_file($TemplateAppViewFile)){
			$ViewFile = $TemplateAppViewFile;
		}
		elseif (is_file($AppViewFile)){
			$ViewFile = $AppViewFile;
		}
		else {
			throw new FMSException("View file(s) not found!", 1);
		}
		
		$Smarty = ApplicationRegistry::getInstance()->get("Smarty");
		
		$Smarty->assign("ApplicationResult", $Result);

		$Smarty->assign("Language", $AppController->getLanguage());
		
		$FetchFile = $Smarty->fetch("file:".$ViewFile);	
		$this->Request->setResult($FetchFile);
	}

}

?>